package action.user;

import java.io.*;

import db.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileDownloadAction extends ActionSupport{
	private String fileName;
	private String inputPath;
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		FileDAO fileDao = new FileDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int f_id = Integer.parseInt(request.getParameter("FId"));
		db.File db_file = fileDao.findById(f_id);
		this.fileName = db_file.getFLocation();
	}

	public String getInputPath() {
		return inputPath;
	}

	public void setInputPath(String inputPath) {
		FileDAO fileDao = new FileDAO();
		HttpServletRequest request = ServletActionContext.getRequest();
		int f_id = Integer.parseInt(request.getParameter("FId"));
		db.File db_file = fileDao.findById(f_id);		
		this.inputPath = db_file.getFLocation();
	}
	
	public InputStream getInputStream() throws Exception {
		//Map session = ActionContext.getContext().getSession();
		//Integer uId = Integer.parseInt((String)session.get("userId")); // Important!!
		
		String file_path = ServletActionContext.getServletContext().getRealPath("/upload"+"\\"+inputPath);
		System.out.println(file_path);
		return ServletActionContext.getServletContext().getResourceAsStream("/upload"+"\\"+inputPath);
	}

	public String details_file(){		
		/*String downloadDir = ServletActionContext.getServletContext().getRealPath("/upload");//+"\\";
		String downloadFile = ServletActionContext.getServletContext().getRealPath("/upload"+inputPath);
		java.io.File file = new java.io.File(downloadFile);
		downloadFile = file.getCanonicalPath();
		if(!downloadFile.startsWith(downloadDir)) {
			return null;
		}*/
		return SUCCESS;
	}
}
